package JZOffer;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class JzOffer32_I {
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int[] ans = new int[1000];
        queue.offer(root);
        int index = 0;

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();

            if (node != null) {
                queue.offer(node.left);
                queue.offer(node.right);
                ans[index++] = node.val;
            }
        }
        return Arrays.copyOfRange(ans, 0, index);
    }
}
